package com.situ.shoplook.common.util;

import lombok.Getter;
import lombok.ToString;

/**
 * 功能：
 *
 * @author 千堆雪
 * @version 1.0.0
 * <p>
 * created by 千堆雪 on 2024/8/24, last modified by 千堆雪 on 2024/8/24
 */
@Getter
@ToString
public class JsonResult<T> {
    private static final JsonResult<?> successEmpty = new JsonResult<>(true, 1200, null);
    private final boolean success;//是否成功
    private final int code;//业务错误码
    private final int status;//http请求的状态码
    private final T data;

    public JsonResult(boolean success, int status, int code, T data) {
        this.success = success;
        this.status = status;
        this.code = code;
        this.data = data;
    }

    private JsonResult(boolean success, int code, T data) {
        this(success, 200, code, data);
    }

    public static <T> JsonResult<T> of(boolean success, T data) {
        return of(success, success ? 1200 : 1500, data);
    }

    public static <T> JsonResult<T> of(boolean success, int code, T data) {
        return new JsonResult<>(success, 200, code, data);
    }

    public static JsonResult<?> success() {
        return successEmpty;
    }

    public static <T> JsonResult<T> success(T data) {
        return of(true, data);
    }

    public static <T> JsonResult<T> fail(T data) {
        return of(false, data);
    }

    public static <T> JsonResult<T> fail(int code, T data) {
        return of(false, code, data);
    }
}
